/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * <p>Project: app2022 - T12
 * <p>Powered by webrx On 2022-02-07 10:35:06
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class T12 {
    static List<String> list = new ArrayList<>();
    static CountDownLatch latch = new CountDownLatch(1);

    static void add() {
        System.out.println("工作线程开始，增加元素");
        for (int i = 0; i < 20; i++) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            String item = "item" + list.size();
            list.add(item);
            System.out.println(item);
            if (list.size() >= 5) {
                latch.countDown();
            }
        }
    }

    static void count() {
        System.out.println("监控线程开始...");
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("集合元素到5个了...");
        System.out.println("监控线程线束了");
    }


    public static void main(String[] args) {
        //工作线程
        new Thread(T12::add, "T1").start();
        //统计线程，（当集合元素到5个就提示输出）
        new Thread(T12::count, "T2").start();
    }
}
